package org.dromara.shopping.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.shopping.domain.ProductAttrResult;
import org.dromara.shopping.mapper.ProductAttrResultMapper;
import org.dromara.shopping.service.IProductAttrResultService;

import java.util.Date;
import java.util.Map;

/**
 * 商品属性详情Service业务层处理
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@RequiredArgsConstructor
@Service
public class ProductAttrResultServiceImpl extends ServiceImpl<ProductAttrResultMapper, ProductAttrResult> implements IProductAttrResultService {

    /**
     * 新增商品属性详情
     * @param map map
     * @param productId 商品id
     */
    @Override
    public void insertProductAttrResult(Map<String, Object> map, Long productId)
    {
        ProductAttrResult yxStoreProductAttrResult = new ProductAttrResult();
        yxStoreProductAttrResult.setProductId(productId);
        yxStoreProductAttrResult.setResult(JSONUtil.toJsonStr(map));
        yxStoreProductAttrResult.setChangeTime(new Date());

        long count = this.count(Wrappers.<ProductAttrResult>lambdaQuery()
            .eq(ProductAttrResult::getProductId,productId));
        if(count > 0) {
            this.remove(Wrappers.<ProductAttrResult>lambdaQuery()
                .eq(ProductAttrResult::getProductId,productId));
        }

        this.save(yxStoreProductAttrResult);
    }

}
